package com.seg.system.service.impl;

import com.seg.common.exception.ServiceException;
import com.seg.common.utils.DateUtils;
import com.seg.common.utils.bean.BeanValidators;
import com.seg.system.domain.TInWarehouse;
import com.seg.system.domain.TSparePart;
import com.seg.system.mapper.TInWarehouseMapper;
import com.seg.system.mapper.TSparePartMapper;
import com.seg.system.service.ITInWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 备件入库Service业务层处理
 *
 * @author seg
 * @date 2023-07-04
 */
@Service
public class TInWarehouseServiceImpl implements ITInWarehouseService {
    @Autowired
    private TInWarehouseMapper tInWarehouseMapper;
    @Autowired
    private TSparePartMapper tSparePartMapper;
    @Autowired
    protected Validator validator;

    /**
     * 查询备件入库
     *
     * @param id 备件入库主键
     * @return 备件入库
     */
    @Override
    public TInWarehouse selectTInWarehouseById(Long id) {
        return tInWarehouseMapper.selectTInWarehouseById(id);
    }

    /**
     * 查询备件入库列表
     *
     * @param tInWarehouse 备件入库
     * @return 备件入库
     */
    @Override
    public List<TInWarehouse> selectTInWarehouseList(TInWarehouse tInWarehouse) {
        return tInWarehouseMapper.selectTInWarehouseList(tInWarehouse);
    }

    /**
     * 新增备件入库
     *
     * @param tInWarehouse 备件入库
     * @return 结果
     */
    @Override
    public int insertTInWarehouse(TInWarehouse tInWarehouse, String operName) {
        tInWarehouse.setCreateTime(DateUtils.getNowDate());
        BeanValidators.validateWithException(validator, tInWarehouse);
        tInWarehouse.setCreateBy(operName);
        tInWarehouse.setDelFlag("0");
        //判断物料状态1，如果当前库存低于安全库存时 ；物料状态2，如果当前库存低于SAP库存时
        //根据物料号查询数据
        TSparePart t = tSparePartMapper.getPartByMaterialNo(tInWarehouse.getMaterialNo());
        if (t == null) {
            return -1;
        }
        Long curStock = t.getStock() + tInWarehouse.getOutNum();
        if (curStock < t.getSapStock()) {
            t.setStatus2("CLSAP");
        } else {
            t.setStatus2("");
        }
        if (curStock < t.getSafetyStock()) {
            t.setStatus1("CLS");
        } else {
            t.setStatus1("");
        }
        t.setStock(curStock);
        int i = tSparePartMapper.updateTSparePart(t);
        int j = tInWarehouseMapper.insertTInWarehouse(tInWarehouse);
        if (i == 0 || j == 0) {
            throw new ServiceException("入库失败");
        }
        return 1;
    }

    /**
     * 修改备件入库
     *
     * @param tInWarehouse 备件入库
     * @return 结果
     */
    @Override
    public int updateTInWarehouse(TInWarehouse tInWarehouse) {
        tInWarehouse.setUpdateTime(DateUtils.getNowDate());
        return tInWarehouseMapper.updateTInWarehouse(tInWarehouse);
    }

    /**
     * 批量删除备件入库
     *
     * @param ids 需要删除的备件入库主键
     * @return 结果
     */
    @Override
    public int deleteTInWarehouseByIds(Long[] ids) {
        return tInWarehouseMapper.deleteTInWarehouseByIds(ids);
    }

    /**
     * 删除备件入库信息
     *
     * @param id 备件入库主键
     * @return 结果
     */
    @Override
    public int deleteTInWarehouseById(Long id) {
        return tInWarehouseMapper.deleteTInWarehouseById(id);
    }
}
